#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/PropertyConfigurator.hh>
using std::cout;
using std::endl;
using namespace log4cpp;

void test(){
    //定义了配置文件所在的位置以及配置文件的名称
    std::string initFileName = "log4cpp.properties";
    //这个类的主要功能就是用来去读取配置文件
    //并且实例化对应的组件对象
    //比如创建PatternLayout、设置conversion pattern等
    //设置appender等
	log4cpp::PropertyConfigurator::configure(initFileName);

    //获取根日志记录器
	log4cpp::Category& root = log4cpp::Category::getRoot();

    //获取叶子记录器
    //其中sub1和sub2需要和配置文件中的名称保持一致
	log4cpp::Category& sub1 = 
		log4cpp::Category::getInstance(std::string("sub1"));

	log4cpp::Category& sub2 = 
		log4cpp::Category::getInstance(std::string("sub2"));

    //之前的代码还需要使用category去关联appender
    //但是此时不需要了

	root.warn("Storm is coming");

	sub1.debug("Received storm warning");
	sub1.info("Closing all hatches");

	sub2.debug("Hiding solar panels");
	sub2.error("Solar panels are blocked");
	sub2.debug("Applying protective shield");
	sub2.warn("Unfolding protective shield");
	sub2.info("Solar panels are shielded");

	sub1.info("All hatches closed");

	root.info("Ready for storm.");

    //资源的释放回收
	log4cpp::Category::shutdown();
}

int main()
{
    test();
    return 0;
}

